package com.example.maoyanmovie.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import androidx.appcompat.app.AppCompatActivity;
import com.example.maoyanmovie.databinding.ActivitySplashBinding;
import com.example.maoyanmovie.utils.Constants;
import com.example.maoyanmovie.utils.SPUtils;

public class SplashActivity extends AppCompatActivity {

    private ActivitySplashBinding binding;
    private static final long SPLASH_DELAY = 2000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            boolean isFirstLaunch = SPUtils.getBoolean(Constants.SP_KEY_FIRST_LAUNCH, true);

            Intent intent;
            if (isFirstLaunch) {
                intent = new Intent(SplashActivity.this, GuideActivity.class);
            } else {
                boolean isLogin = SPUtils.getBoolean(Constants.SP_KEY_IS_LOGIN, false);
                if (isLogin) {
                    intent = new Intent(SplashActivity.this, MainActivity.class);
                } else {
                    intent = new Intent(SplashActivity.this, LoginActivity.class);
                }
            }

            startActivity(intent);
            finish();
        }, SPLASH_DELAY);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
